--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[



]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 
    local Widget = require "widgets/widget"
    local Image = require "widgets/image" -- 引入image控件
    local UIAnim = require "widgets/uianim"


    local Screen = require "widgets/screen"

    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local TextButton = require "widgets/textbutton"
    local UIAnimButton = require "widgets/uianimbutton"

    local Button = require "widgets/button"

    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"

    local Badge = require "widgets/badge"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 坐标读取
    local function GetHUDLoation()
        local data = TUNING.HUTAO_FNS:Get_ThePlayer_Cross_Archived_Data("physical_power_hud_location")
        if data == nil then
            return 0.83802083333333,0.8141592920354
        else
            return data.x,data.y
        end
    end
    local function SetHUDLoation(x,y)
        TUNING.HUTAO_FNS:Set_ThePlayer_Cross_Archived_Data("physical_power_hud_location",{x = x,y = y})
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local function badge_setup(inst)
        ----------------------------------------------------------------------------------------------------------------
        --- 前置根节点
            local front_root = inst.HUD and inst.HUD.controls.status
            if front_root == nil then
                print("Error : physical_power_badge setup failed")
                return
            end
        ----------------------------------------------------------------------------------------------------------------
        --- 创建根节点
        ----------------------------------------------------------------------------------------------------------------
        --- 创建盾牌框框
            local anim = nil
            local owner = ThePlayer
            local tint = { 148 / 255, 0 / 255, 211 / 255, 1 }
            local iconbuild = "status_hunger"
            local circular_meter = true
            local use_clear_bg = true
            local dont_update_while_paused = true
            local root = front_root:AddChild(Badge(anim, owner, tint, iconbuild, circular_meter, use_clear_bg, dont_update_while_paused))
            root.circular_meter:GetAnimState():Pause() -- 暂停动画
        ----------------------------------------------------------------------------------------------------------------
        --- 重置基点
            root:SetHAnchor(1) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
            root:SetVAnchor(2) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
            root:SetPosition(1000,500)
            -- root:FollowMouse()
            --- 刷一次缩放。保证和当前HUD缩放一致
            local old_scale = root:GetScale()
            local ret_scale =  old_scale.x
            root:SetScale(ret_scale, ret_scale, ret_scale)
        ----------------------------------------------------------------------------------------------------------------
        --- 
            local temp_text = root:AddChild(Text(CODEFONT,30,"体力值",{ 255/255 , 255/255 ,255/255 , 1}))
            temp_text:SetPosition(0,40,0)
        ----------------------------------------------------------------------------------------------------------------
        --- 启动坐标跟随缩放循环任务，缩放的时候去到指定位置。官方好像没预留这类API，或者暂时找不到方法
            function root:LocationScaleFix()
                if self.x_percent and not self.__mouse_holding  then
                    local scrnw, scrnh = TheSim:GetScreenSize()
                    if self.____last_scrnh ~= scrnh then
                        local tarX = self.x_percent * scrnw
                        local tarY = self.y_percent * scrnh
                        self:SetPosition(tarX,tarY)
                    end
                    self.____last_scrnh = scrnh
                end
            end            
            root.x_percent,root.y_percent = GetHUDLoation()
            root:LocationScaleFix()
            owner:DoPeriodicTask(2,function()
                root:LocationScaleFix()
            end)
        ----------------------------------------------------------------------------------------------------------------
        ---- 鼠标拖动
            local old_OnMouseButton = root.OnMouseButton
            root.OnMouseButton = function(self,button, down, x, y)
                if down and button == MOUSEBUTTON_RIGHT then
                    if not root.__mouse_holding  then
                        root.__mouse_holding = true      --- 上锁
                            --------- 添加鼠标移动监听任务
                            root.___follow_mouse_event = TheInput:AddMoveHandler(function(x, y)  
                                root:SetPosition(x,y,0)
                            end)
                            --------- 添加鼠标按钮监听
                            root.___mouse_button_up_event = TheInput:AddMouseButtonHandler(function(button, down, x, y) 
                                if button == MOUSEBUTTON_RIGHT and down == false then    ---- 左键被抬起来了
                                    root.___mouse_button_up_event:Remove()       ---- 清掉监听
                                    root.___mouse_button_up_event = nil

                                    root.___follow_mouse_event:Remove()          ---- 清掉监听
                                    root.___follow_mouse_event = nil

                                    root:SetPosition(x,y,0)         ---- 设置坐标
                                    root.__mouse_holding = false    ---- 解锁

                                    local scrnw, scrnh = TheSim:GetScreenSize()
                                    root.x_percent = x/scrnw
                                    root.y_percent = y/scrnh
                                    SetHUDLoation(root.x_percent,root.y_percent)

                                end
                            end)
                    end

                end
                return old_OnMouseButton(self,button, down, x, y)
            end
        ----------------------------------------------------------------------------------------------------------------
        --- 外框修改
            -- root.circleframe:GetAnimState():OverrideSymbol("frame_circle","loramia_status_meter","frame_circle")

        ----------------------------------------------------------------------------------------------------------------
        --- 图标修改
            -- root.circleframe:GetAnimState():Hide("icon")
            -- local icon = root.circleframe:AddChild(UIAnim())
            -- icon:GetAnimState():SetBank("loramia_status_meter")
            -- icon:GetAnimState():SetBuild("loramia_status_meter")
            -- icon:GetAnimState():PlayAnimation("recharge")
            -- local icon_sacle = 1
            -- icon:SetScale(icon_sacle,icon_sacle,icon_sacle)
            -- icon:SetPosition(0,-1,0)
        ----------------------------------------------------------------------------------------------------------------
        --- 自适应寻找 最大值节点
            local function find_all_max_node(t)
                local ret = {}
                -- 遍历table t中的每个键值对
                for k, v in pairs(t) do
                    -- 检查key是否包含'max'并且value含有SetString函数
                    if type(k) == "string" and type(v) == "table" and string.match(k, "max") ~= nil and type(v.SetString) == "function" then
                        table.insert(ret, v)
                    end
                end
                return ret -- 如果没有找到，返回nil
            end
        ----------------------------------------------------------------------------------------------------------------
        ---
            -- root:SetPercent(0.5)
            local display_max_nodes = nil
            local function value_refresh_fn()                
                local current = inst.replica.hutao_com_physical_power:GetCurrent()
                local max = inst.replica.hutao_com_physical_power:GetMax()
                root:SetPercent(current/max,max)
                if display_max_nodes == nil then
                    display_max_nodes = find_all_max_node(root)
                end
                for _,node in pairs(display_max_nodes) do
                    node:SetString(tostring(max))
                end
            end
            value_refresh_fn()
            root.inst:ListenForEvent("hutao_com_physical_power_update",value_refresh_fn,inst)
        ----------------------------------------------------------------------------------------------------------------
        ---
        ----------------------------------------------------------------------------------------------------------------
        return root
        ----------------------------------------------------------------------------------------------------------------
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

return function(inst)
    inst:DoTaskInTime(1,function()
        if ThePlayer == inst and inst.HUD then
            badge_setup(inst)
        end
    end)
    if not TheWorld.ismastersim then
        return
    end

end